﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace CVsharp.Jquery.Tree
{
    public class Node
    {
        /// <summary>
        /// 节点属性
        /// </summary>
        public Dictionary<string, object> Propertys { get; set; }
        /// <summary>
        /// 父节点
        /// </summary>
        public Node Parent { get; set; }
        /// <summary>
        /// 动态节点属性
        /// </summary>
        public dynamic DPropertys { get; set; }
        private IList<Node> _nodes;
        /// <summary>
        /// 节点集合
        /// </summary>
        public IList<Node> Nodes
        {
            get { return _nodes; }
            set
            {
                foreach (var node in value)
                {
                    node.Parent = this;
                }
                _nodes = value;
            }
        }
        public Node()
        {
            Nodes = new List<Node>();
            Propertys = new Dictionary<string, object>();
            DPropertys = new DPropertys(Propertys);
        }

    }

    public class DPropertys : DynamicObject
    {
        public Dictionary<string, object> Dic { get; set; }
        public DPropertys(Dictionary<string, object> dic)
        {
            Dic = dic;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var name = binder.Name;
            return Dic.TryGetValue(name, out result);
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            var name = binder.Name;
            Dic[name] = value;
            return true;
        }
    }
}
